package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.po.News;
import com.example.demo.service.NewService;


@Controller
@RequestMapping("/admin")
public class AdminNewController {
	
	@Autowired	
	private NewService newService;
	
	@RequestMapping(value="/adminNews.action",method=RequestMethod.GET)
	public String indext(Model model){
		
		List<News> newsList = newService.selectAll();
		
		model.addAttribute("newsList", newsList); 
		
		return "admin/news/newsList";
		
	}
	
	@RequestMapping(value="/toEditNews.action",method=RequestMethod.GET)
	public String toEdit(Model model,Integer id){
		
		if(id!=null){
			
			News news=newService.selectById(id);
			
			model.addAttribute("news", news);
		}
		
		return "admin/news/edit";
		
	}
	
	@RequestMapping(value="/editNews.action",method=RequestMethod.POST)
	public String edit(News news){
		
		if(news.getId()!=null){
			news.setTime(new Date());
			newService.update(news);
		}else{
			news.setTime(new Date());
			newService.insert(news);
		}
		
		return "redirect:adminNews.action";		
	}
	
	@RequestMapping(value="/deleteNews.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		newService.delete(id);
		return "redirect:adminNews.action";
		
	}

}
